import { resolve } from 'path'
import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import VueJSX from '@vitejs/plugin-vue-jsx'
import AutoImport from 'unplugin-auto-import/vite'
import vueSetupExtend from 'vite-plugin-vue-setup-extend'
import Less2CssVariablePlugin from 'antd-less-to-css-variable'
import viteCompression from 'vite-plugin-compression'

export const r = (...args) => resolve(__dirname, '.', ...args)

export default defineConfig(({ mode }) => ({
	server: {
		port: loadEnv(mode, './').VITE_PORT
	},
	resolve: {
		alias: {
			'~': `${resolve(__dirname, './')}`,
			'@/': `${resolve(__dirname, 'src')}/`
		}
	},
	// 解决警告You are running the esm-bundler build of vue-i18n.
	define: {
		__VUE_I18N_FULL_INSTALL__: true,
		__VUE_I18N_LEGACY_API__: true,
		__VUE_I18N_PROD_DEVTOOLS__: true
	},
	build: {
		// sourcemap: true,
		manifest: true, // 生产文件名称与Hash后的文件名称对应mpa
		brotliSize: false,
		rollupOptions: {
			output: {
				manualChunks: {
					// 将这些依赖单独创建为一个文件
					echarts: ['echarts'],
					'ant-design-vue': ['ant-design-vue'],
					vue: ['vue', 'vue-router', 'pinia', 'vue-i18n']
				}
			}
		},
		chunkSizeWarningLimit: 1500
	},
	plugins: [
		vue(),
		viteCompression(),
		vueSetupExtend(),
		VueJSX(),
		AutoImport({
			imports: ['vue'],
			dirs: ['./src/utils/permission'],
			dts: r('src/auto-imports.d.ts')
		})
	],
	css: {
		preprocessorOptions: {
			less: {
				javascriptEnabled: true,
				plugins: [new Less2CssVariablePlugin()]
			}
		}
	},
	optimizeDeps: {}
}))
